<script setup lang="ts">
import { ref , nextTick} from 'vue'
import { getLodop } from '@/lib/lodop/LodopFuncs'
import { useUserStore } from '@/store/modules/user-store'
import printModal from '@/components/business/print-modal/index.vue'
import { purchaseOrderApi } from '@/api/business/orders/purchase-order-api'
import { useEnums } from '@/hooks/plugins/useEnums'
import dayjs from 'dayjs'
const { getDescByValue } = useEnums()
const emit = defineEmits(['close', 'getList'])
const open = ref<boolean>(false)

const companyInfo = useUserStore().getCompanyInfo
const userInfo = useUserStore().getUserInfo

const visible = ref(false)
// 详情
const options = ref([])
defineExpose({
  showDrawer
})
function showDrawer(item) {
  options.value = item
  visible.value = true
}
function onClose() {
  visible.value = false
}
const loading = ref(false)
const usePrint = () => {
  
  const onPrint = async () => {
    loading.value = true
try {
    const res = await purchaseOrderApi.detail({ id: options.value.id, is_print: true })

    const newCount = res.data.printLog.print_count
    if (!options.value.printLog) options.value.printLog = {}
    options.value.printLog.print_count = newCount
  } catch (e) {
    console.error('更新打印次数失败', e)
    loading.value = false
    return
  }

  await nextTick()

    const LODOP = getLodop()
    if (!LODOP) {
      open.value = true
      loading.value = false
      return
    }
    LODOP.PRINT_INITA('0mm', '0mm', '241mm', '93mm', '采购单')
    LODOP.SET_PRINT_PAGESIZE(1, '215.9mm', '93mm', '采购单')
    LODOP.ADD_PRINT_TABLE(20, '3%', '94%', '100%', document.querySelector('#printPurchase')?.outerHTML)
    LODOP.SET_PRINT_STYLEA(0, 'TableRowThickNess', '12pt')
    LODOP.ADD_PRINT_TEXT(30, '3%', 196, 20, '县链云仓')
    LODOP.SET_PRINT_STYLEA(0, 'ItemType', 0)
    LODOP.NewPageA()
    LODOP.PREVIEW()
    loading.value = false
    
  }
  return {
    onPrint
  }
}
const { onPrint } = usePrint()
const handleModalClose = () => {
  open.value = false
}
</script>

<template>
  <printModal :open="open" @cancel="handleModalClose" />
  <a-drawer title="采购单打印" :open="visible" @close="onClose">
    <template #closeIcon>
      <close @on-close="onClose" />
    </template>
    <a-button class="absolute top-0 right-0 z-2" type="primary" @click="() => onPrint()" >补单打印</a-button>
    <table id="printPurchase" border="0" center cellSpacing="0" cellPadding="1" width="100%" style="border-collapse: collapse; font-size: 11pt">
      <tbody>
        <tr>
    <td colspan="8">
            <div style="line-height: 30px; text-align: center; font-size: 16pt; font-weight: 700">{{ companyInfo?.name || '-' }}采购单</div>
            <font style="position: absolute; right: 0; top: 0">
              <span tdata="pageNO" format="#">#</span>
              <span>/</span>
              <span tdata="pageCount" format="#">##</span>
            </font>
          </td>
  </tr>
        </tbody>
      <tbody>
        <tr>
          <td colspan="8">供应商: {{ options.supplier?.name }}&nbsp;&nbsp;&nbsp;&nbsp;入库仓库: {{ options.warehouse?.name }}{{ getDescByValue('WAREHOUSE_ATTRIBUTE_TYPE', options.warehouse?.attribute_type) }}&nbsp;&nbsp;&nbsp;&nbsp;订单金额: {{ Number(options?.total_price).toFixed(2) }}元&nbsp;&nbsp;&nbsp;&nbsp;采购日期: {{ options?.create_time }}&nbsp;&nbsp;&nbsp;&nbsp;打印次数:{{ options?.printLog?.print_count || 0 }}&nbsp;&nbsp;&nbsp;&nbsp;打印日期: {{ dayjs(Date()).format('YYYY-MM-DD HH:mm:ss') }}&nbsp;&nbsp;&nbsp;&nbsp;单据编号: {{ options.no }}</td>
        </tr>
        <tr>
          <td width="5%" style="border: 1pt solid #000">
            <div align="center"><b>序号</b></div>
          </td>
          <td style="border: 1pt solid #000">
            <div align="center"><b>商品名称</b></div>
          </td>
          <td width="10%" style="border: 1pt solid #000">
            <div align="center"><b>商品条码</b></div>
          </td>
          <td width="5%" style="border: 1pt solid #000">
            <div align="center"><b>单位</b></div>
          </td>
          <td width="7%" style="border: 1pt solid #000">
            <div align="center"><b>数量</b></div>
          </td>
          <td width="7%" style="border: 1pt solid #000">
            <div align="center"><b>采购单价</b></div>
          </td>
          <td width="10%" style="border: 1pt solid #000">
            <div align="center"><b>小计</b></div>
          </td>
          <td style="border: 1pt solid #000">
            <div align="center"><b>备注</b></div>
          </td>
        </tr>
        <tr v-for="(currItem, index) in options?.detail" :key="currItem.id">
          <td style="border: 1pt solid #000; text-align: center">{{ index + 1 }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ currItem.goods?.goods_name }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ currItem.spec?.barcode }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ currItem.spec?.unit_name }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ currItem.quantity }} {{ currItem.spec?.unit_name }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ Number(currItem.purchase_price).toFixed(2) }}元</td>
          <td style="border: 1pt solid #000; text-align: center">{{ currItem.purchase_total_price }}元</td>
          <td style="border: 1pt solid #000; text-align: center">{{ currItem.remark }}</td>
        </tr>
        <tr>
          <td style="border: 1pt solid #000; text-align: center">总计</td>
          <td style="border: 1pt solid #000">&nbsp;</td>
          <td style="border: 1pt solid #000">&nbsp;</td>
          <td style="border: 1pt solid #000">&nbsp;</td>
          <td style="border: 1pt solid #000; text-align: center">{{ options.detail?.reduce((prev, cur) => prev + cur.quantity, 0) }}</td>
          <td style="border: 1pt solid #000">&nbsp;</td>
          <td style="border: 1pt solid #000; text-align: center">{{ options.detail?.reduce((prev, cur) => prev + Number(cur.purchase_total_price), 0).toFixed(2) }}元</td>
          <td style="border: 1pt solid #000">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="8" style="font-weight: 700">制单员: {{ userInfo?.username ?? userInfo.phone }}&nbsp;&nbsp;&nbsp;&nbsp; 备注：{{ options?.remark }}</td>
        </tr>
      </tbody>
    </table>
    
    <template #footer>
      <div class="flex justify-end">
        <a-button type="default" @click="onClose" style="margin-right: 8px">关闭</a-button>
        <a-button :loading="loading" type="primary" @click="onPrint">打印</a-button>
      </div>
    </template>
  </a-drawer>
</template>
